package main.edu.csu.shan.util;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 汉字处理相关工具类
 */
public class HanUtils {
    // 识别非中文符号  包括英文，标点，数学运算符
    public static boolean isChineseCharacter(String text) {   // 可以识别繁体字
        // 中文验证规则
        String regEx = "[\\u4e00-\\u9fa5]+";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();  // 字符串是否与正则表达式相匹配
    }


    /**
     * 判断两个字符串首字符是否相等
     */
    public static boolean hasCommonFirstCharacter(String str1, String str2) {
        if (StringUtils.isBlank(str1) || StringUtils.isBlank(str2)) {
            return false;
        }
        return str1.substring(0, 1).equals(str2.substring(0, 1));
    }


    /**
     * 取中文词汇每个字转换成拼音后的首字母集合  如 张三丰 ->  zsf
     */
    public static String firstPinyinCharStr(String chineseWord) {
        StringBuffer pinyinName = new StringBuffer();
        char[] nameChar = chineseWord.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
       /* for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    // 取得当前汉字的所有全拼
                    String[] strs = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                    // System.out.println(Arrays.toString(strs));
                    if (strs != null) {
                        //  for (int j = 0; j < strs.length; j++) {
                        for (int j = 0; j < 1; j++) {
                            // 取首字母
                            pinyinName.append(strs[j].charAt(0));
                            if (j != strs.length - 1) {
                                pinyinName.append(",");
                            }
                        }
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName.append(nameChar[i]);
            }
            pinyinName.append(" ");
        }    //return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));*/
       for(char cha : nameChar){
           try {
               String[] strs = PinyinHelper.toHanyuPinyinStringArray(cha, defaultFormat);
               pinyinName.append(strs[0]);
              // System.out.println(Arrays.toString(strs));
           } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
               badHanyuPinyinOutputFormatCombination.printStackTrace();
           }
       }
        //System.out.println(Arrays.toString(strs));
        return pinyinName.toString();
      //  return pinyinName.toString().replaceAll(",", "").replaceAll(" ", "");
    }


    public static void main(String[] args) {
        System.out.println(firstPinyinCharStr("单俊维"));
    }
}
